$(function () {
    let markid = '';
    let code = getUrlString('code');

    $('.getBtn').click(function () {
        if (markid === ''){
            $('#helpWrap').show();
        } else {
            exchange()
        }
    });

    $('#indexBack').click(function () {
        $('#conAlert').hide();
    });

    $('#helpWrapAlert').click(function () {
        $('#helpWrap').hide();
    });

    $('#gotoAppBtn').click(function () {
        window.location.href = 'https://www.odancool.com'
    });

    //获取验证码
    $('#getCode').click(function () {
        getCode();
    });

    $('#loginBtn').click(function () {
        let phone = $('#phoneNum').val();
        let phoneCode = $('#phoneCode').val();
        login(phone,phoneCode)
    });

    function login (phone,pw) {
        if (phone === ''){
            laymsg('手机号不能为空');
            return
        }
        if (pw === ''){
            laymsg('验证码不能为空');
            return
        }
        let data = {
            'phone':phone,
            'pw':pw,
            'openid1':'',
            'nan':'',
        };
        getData('post', '/niu/api/member_login_phone.php', data, function (data) {
            console.log(data);
            if (data.resultcode === '0'){
                markid = data.markid;
                $('#phoneNum').val('');
                $('#phoneCode').val('');
                $('#helpWrap').hide();
                exchange()
            } else {
                laymsg(data.msg)
            }
        })
    }

    function exchange() {
        let data = {
            'type':'doexchange',
            'shortid':code,
            'markid':markid
        };
        getData('get', '/niu/apiv4/niu_exchange_money3.php', data, function (data) {
            console.log(data);
            if (data.resultcode === '200'){
                $('#conAlert .info').html('领取成功');
                setTimeout(function () {
                    window.location.href = 'https://www.odancool.com'
                },2000);
                $('#conAlert').show()
            } else {
                $('#conAlert .info').html(data.msg);
                setTimeout(function () {
                    window.location.href = 'https://www.odancool.com'
                },2000);
                $('#conAlert').show()
            }

        });
    }

    function getCode () {

        let phone = $('#phoneNum').val();
        if (!phoneNumberVer(phone)){
            return
        }

        let str = getCurrentTime()+phone;
        let jiaMi = btoa(str);
        let jiaMi2 = btoa(jiaMi);
        let signStr = randomWord(false,8);
        let sign = signStr + jiaMi2;
        let data = {
            'phone':phone,
            'sign':sign
        };
        getData('post', '/niu/api/member_login_smsv2.php', data, function (data) {
            console.log(data);
            if (data.resultcode === '0'){
                $('#getCode').hide();
                $('#getCodeNum').show();
                var timer = null;
                var count = 60;
                timer = setInterval(function(){
                    count--;
                    $('#getCodeNum').html(count+'S');
                    if (count <=0) {
                        clearInterval(timer);
                        $('#getCodeNum').hide();
                        $('#getCode').show();
                    }
                },1000);
            } else {
                laymsg(data.msg)
            }
        })
    }

    /*
    ** randomWord 产生任意长度随机字母组合
    */

    function randomWord(randomFlag, min, max){
        var str = "",
            range = min,
            arr = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

        // 随机产生
        if(randomFlag){
            range = Math.round(Math.random() * (max-min)) + min;
        }
        for(var i=0; i<range; i++){
            pos = Math.round(Math.random() * (arr.length-1));
            str += arr[pos];
        }
        return str;
    }

    //获取当前时间
    function getCurrentTime() {
        var date = new Date();//当前时间
        var year = date.getFullYear();
        var month = zeroFill(date.getMonth() + 1);//月
        var day = zeroFill(date.getDate());//日
        var hour = zeroFill(date.getHours());//时
        var minute = zeroFill(date.getMinutes());//分
        var second = zeroFill(date.getSeconds());//秒

        //当前时间
        var curTime = String(year) + String(month) + String(day)+String(hour) + String(minute) + String(second);

        return curTime;
    }
});
